package com.baiyun.service.impl;


import com.baiyun.Result.Result;
import com.baiyun.entity.Inventory;
import com.baiyun.entity.Orders;
import com.baiyun.entity.Sale;
import com.baiyun.entity.Tea;
import com.baiyun.mapper.InventoryMapper;
import com.baiyun.mapper.OrderMapper;
import com.baiyun.mapper.SalesMapper;
import com.baiyun.mapper.TeaMapper;
import com.baiyun.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.sql.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class OrderServiceImpl implements OrderService {

    private final OrderMapper ordersMapper;
    private final InventoryMapper inventoryMapper;
    private final SalesMapper salesMapper;

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Orders> selectAllOrder() {
        return ordersMapper.selectAllOrder();
    }

    /**
     * 添加订单
     * @param orders
     */
    @Override
    public void addOrder(Orders orders) {
        orders.setTimes(new Date(System.currentTimeMillis()));
        orders.setNote("已支付");
        ordersMapper.addOrder(orders);
    }

    /**
     * 更新订单
     * @param orders
     */
    @Override
    public void updateOrder(Orders orders) {
        ordersMapper.updateOrder(orders);
    }

    /**
     * 删除订单
     * @param oid
     */
    @Override
    public void deleteOrder(Integer oid) {
        ordersMapper.deleteOrder(oid);
    }

    /**
     * 根据id查询订单
     * @param oid
     * @return
     */
    @Override
    public Orders selectById(Integer oid) {
        return ordersMapper.selectById(oid);
    }

    /**
     * 根据茶名模糊搜索订单
     * @param tname
     * @return
     */
    @Override
    public List<Orders> slectByTeaName(String tname) {
        return ordersMapper.slectByTeaName(tname);
    }

    @Override
    public void UserAddOrder(Orders orders) {
        Date now = new Date(System.currentTimeMillis());
        orders.setTimes(now);
        orders.setNote("已支付");
        ordersMapper.addOrder(orders);

        Integer tid = orders.getTid();
        Inventory inventory = inventoryMapper.selectInventoryByTid(tid);
        if(inventory.getNum()<=orders.getNum()){
            throw new RuntimeException("库存不足");
        }
        inventory.setNum(inventory.getNum()-orders.getNum());
        if (inventory.getNum()-orders.getNum()==0){
            inventory.setStatus("无库存");
        }
        inventoryMapper.updateInventory(inventory);

        Sale sale = new Sale();
        sale.setOid(ordersMapper.selectOrder(orders).getOid());
        sale.setTid(tid);
        sale.setName(orders.getTname());
        sale.setType(orders.getType());
        sale.setPrice(orders.getPrice());
        sale.setNum(orders.getNum());
        sale.setAllprice(orders.getAllprice());
        sale.setPaytype(orders.getPaytype());
        sale.setTime(new Date(System.currentTimeMillis()));
        salesMapper.addSales(sale);
    }


}

